package handle

import (
	"bytes"
	"fmt"
	"github.com/PuerkitoBio/goquery"
	"log"
	"strings"
)

func Doc(data []byte) *goquery.Document {
	reader := bytes.NewReader(data)
	//r io.Reader
	doc, err := goquery.NewDocumentFromReader(reader)
	if err != nil {
		log.Fatal(err)
	}
	return doc
}
func Handle2(doc *goquery.Document) []string {
	// Find the review items
	var handles []string

	doc.Find(".game_image").Each(func(i int, selection *goquery.Selection) {
		img, _ := selection.Find("a").Html()

		img = strings.ReplaceAll(strings.ReplaceAll(img, "<img src=\"", ""), "\"/>", "")
		img = strings.ReplaceAll(strings.ReplaceAll(img, "\n", ""), "\t", "")
		img = strings.ReplaceAll(strings.ReplaceAll(img, "\\\" class=\\\"visible", ""), " ", "")
		img = strings.ReplaceAll(img, "\"class=\"visible", "")
		fmt.Println(img)

		handles = append(handles, img)
	})

	return handles
}
func Handle(doc *goquery.Document) []Feature {
	var handles []Feature
	// Find the review items
	doc.Find("#about_steam_features_grid .feature").Each(func(i int, s *goquery.Selection) {
		img, err := s.Find(".about_feature_icon").Html()

		if err != nil {
			panic(err)

		}
		feature_title := s.Find(".feature_title").Text()
		feature_text := s.Find(".feature_text").Text()
		img = strings.ReplaceAll(strings.ReplaceAll(img, "<img src=\"", ""), "\"/>", "")
		fmt.Println(img)

		icon := Feature{
			strings.ReplaceAll(strings.ReplaceAll(feature_title, "\n", ""), "\t", ""),
			strings.ReplaceAll(strings.ReplaceAll(feature_text, "\n", ""), "\t", ""),
			strings.ReplaceAll(strings.ReplaceAll(img, "\n", ""), "\t", ""),
		}
		handles = append(handles, icon)
	})
	return handles
}

type Feature struct {
	Title,
	Text,
	Img string
}
